//
//  UIColor+HexString.m
//  Categorys
//
//  Created by taihankeji on 2017/5/16.
//  Copyright © 2017年 taihankeji. All rights reserved.
//

#import "UIColor+Commons.h"

@implementation UIColor (Commons)

+ (UIColor *)colorwithHexString:(NSString *)hexString {
    return [UIColor colorwithHexString:hexString alpha:1.0];
}

+ (UIColor *)colorwithHexString:(NSString *)hexString alpha:(CGFloat)alpha {
    //删除字符串中的空格
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    //只支持六位的十六进制颜字符串
    if (cString.length < 6) {
        return [UIColor clearColor];
    }
    
    //如果是"0X"开头 从第2位开始截取字符串到末尾
    if ([cString hasPrefix:@"0X"]) {
        [cString substringFromIndex:1];
    }
    
    //如果是"#"开头 从第1位开始截取字符串到末尾
    if ([cString hasPrefix:@"#"]) {
        [cString substringFromIndex:1];
    }
    
    //分割字符串cString 得到 r, g, b值
    NSRange range;
    range.location = 0;
    range.length = 2;
    //获取rString
    NSString *rString = [cString substringWithRange:range];
    
    //获取gString
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //获取bString
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

+ (UIColor *)randomColor {
    NSInteger aRedValue = arc4random() % 225;
    NSInteger aGreenValue = arc4random() % 255;
    NSInteger aBlueValue = arc4random() % 255;
    return [UIColor colorWithRed:aRedValue / 255.0f green:aGreenValue / 255.0f blue:aBlueValue / 255.0f alpha:1.0];
}



+ (UIColor *)gradientColorWithStarColor:(UIColor *)starColor endColor:(UIColor *)endColor Size:(CGSize)size gradientDirection:(GradientColorDirection)direction {
   
    //默认自上而下的初始值
    CGSize beginSize = CGSizeMake(0.1, size.height);
    CGPoint beginPoint = CGPointMake(0, 0);
    CGPoint endPoint = CGPointMake(0, size.height);
    
    switch (direction) {
        case GradientColorDirectionDefault:
        {
            beginSize = CGSizeMake(0.1, size.height);
            beginPoint = CGPointMake(0, 0);
            endPoint = CGPointMake(0, size.height);
        }
            break;
        case GradientColorDirectionDownToUp:
        {
            beginSize = CGSizeMake(0.1, size.height);
            beginPoint = CGPointMake(0, size.height);
            endPoint = CGPointMake(0, 0);
        }
            break;
        case GradientColorDirectionLeftToRight:
        {
            beginSize = CGSizeMake(size.width, 0.1);
            beginPoint = CGPointMake(0, 0);
            endPoint = CGPointMake(size.width, 0);
        }
            break;
        case GradientColorDirectionRightToLeft:
        {
            
            beginSize = CGSizeMake(size.width, 0.1);
            beginPoint = CGPointMake(size.width, 0);
            endPoint = CGPointMake(0, 0);

        }
            break;
        
    }
    
    UIGraphicsBeginImageContextWithOptions(beginSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    NSArray* colors = [NSArray arrayWithObjects:(id)starColor.CGColor, (id)endColor.CGColor, nil];
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
    CGContextDrawLinearGradient(context, gradient, beginPoint, endPoint,  0);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:image];

}

- (UIImage *)getImage {
   
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [self CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end
